import React, { Component } from 'react';
import {
	StyleSheet, Text, View, TouchableOpacity, Image, FlatList, TextInput, PixelRatio, Alert
} from "react-native";
import { GLOBAL } from '../../../config/global';
import { PageStatusIndex } from '../../../component/page-status/PageStatusIndex';
import { API, API_GENERATE } from '../../../config/api';
import HTTP from '../../../utils/http';
import { style } from '../../../theme/style';
import { Common } from '../../../component/common/Common.js';
import StorageHelper from '../../../utils/storageHelper';
import { STORAGE_CONSTANT } from '../../../config/storage';
import navigationSeevice from '../../../components/navigation/navigationSeevice';

const scale = GLOBAL.SCALE;
const searchIcon = require('../../../imgs/global/whiteSearch.png');
export class PurchasingRules extends Component {
	static navigationOptions = ({ navigation, navigationOptions }) => {
		return {
			headerTitle: '采销规则',
			headerTintColor: style.color.white,
			headerLeft: <Common type="headLeft" onPress={() => { navigation.goBack() }} color='#fff' />,
			headerRight: (<Image style={style.head.headRightEmpty} source={require('../../../imgs/goBackEmpty.png')} />),
			headerStyle: {  // 头部整体样式 
				elevation: 0,
				borderBottomWidth: 0,
				backgroundColor: style.color.themeColor
			},
			headerTitleStyle: {
				flex: 1,
				textAlign: 'center'
			}
		}
	};

	constructor(props) {
		super(props)
		let { salesType, bizModel, selectKey } = props.navigation.state.params || {};
		this.state = {
			status: 'data',
			listData: [],
			refreshIcon: false, // 下拉刷新图标
		};
		this.pageParams = { // TODO
			salesType: salesType,
			bizModel: bizModel,
		};
		this.searchKey = '';
		this.selectKey = selectKey
	}

	async componentWillMount() {
		this.initPage();
	}

	async initPage() {
		this.setState({
			status: 'loading'
		})
		this.pageParams.searchKey = this.searchKey
		let res = await HTTP.POST(API_GENERATE(API.PACKAGE_RULE), this.pageParams);
		if (res) {
			this.setState({
				status: res && res.length > 0 ? 'data' : 'empty',
				listData: res,
			})
		} else {
			this.setState({
				status: 'error'
			})
		}
	}
	// 刷新页面
	reloadPage() {
		this.initPage();
	}

	_onErrorRefresh = () => {
		this.initPage();
	}

	// 搜索View
	_renderFilterWrap() {
		return (
			<View style={styles.filterWrap}>
				<View>
					<Image source={searchIcon} style={styles.searchIcon} />
					<TextInput
						ref="enterpriseInput"
						style={styles.searchInput}
						underlineColorAndroid='transparent'
						placeholder={"请输入规则名称"}
						placeholderTextColor="rgba(255, 255, 255, .8)"
						maxLength={30}
						returnKeyType={'search'}
						defaultValue={this.searchKey}
						onChangeText={(txt) => { this.searchKey = txt; }}
						onSubmitEditing={() => { this.reloadPage(); }}
					/>
				</View>
			</View>
		)
	}

	callBackfuc = (data) => {
		if (this.selectKey) {
			Alert.alert(
				'提示',
				"采销规则修改会导致商品、定向客户及其他关联内容被清空，是否确认",
				[
					{
						text: "取消"
					},
					{
						text: "确认", onPress: () => {
							let { callback } = this.props.navigation.state.params
							callback(data)
							this.props.navigation.goBack()
						}
					}
				]
			);
		}
		else {
			let { callback } = this.props.navigation.state.params
			callback(data)
			this.props.navigation.goBack()
		}
	}
	_renderListItem = (data) => {
		let { ruleName, salesRuleId, goodsCateId, goodsCateName } = data.item
		return (
			<View style={styles.clickButtom}>
				<Text>{ruleName}</Text>
				<Text style={{ color: style.color.themeColor }} onPress={() => {
					this.callBackfuc({ salesRuleId, ruleName, goodsCateId, goodsCateName })
				}}>添加</Text>
			</View>
		)
	}

	render() {
		let { status, listData, refreshIcon, } = this.state;
		return (
			<View style={styles.container}>
				{this._renderFilterWrap()}
				{
					status == 'data' ? (<View style={{ flex: 1, paddingHorizontal: 15 * scale, marginTop: 10 * scale, }}>
						<FlatList
							data={listData}
							renderItem={this._renderListItem}
							refreshing={refreshIcon}
							onRefresh={() => { this.reloadPage() }}
							onEndReachedThreshold={1}
						/>
					</View>) : null
				}
				{
					status != 'data' ? (<PageStatusIndex status={status} emptyText={'暂无记录'} onErrorPress={this._onErrorRefresh} />) : null
				}
			</View>
		)
	}
}
const styles = StyleSheet.create({
	container: {
		flex: 1,
		backgroundColor: style.color.bgGray,
		paddingBottom: 30 * scale
	},
	filterWrap: {
		flexDirection: 'row',
		alignItems: 'center',
		justifyContent: 'center',
		backgroundColor: style.color.themeColor,
		paddingTop: 14 * scale,
		paddingBottom: 14 * scale,
	},
	searchInput: {
		height: 36 * scale,
		width: 270 * scale,
		paddingLeft: 80 * scale,
		paddingRight: 12 * scale,
		backgroundColor: 'rgba(255, 255, 255, .18)',
		flexDirection: 'row',
		justifyContent: 'center',
		alignItems: 'center',
		paddingVertical: 0,
		position: 'relative',
		borderRadius: 8 * scale,
		color: '#fff',
	},
	searchIcon: {
		width: 20 * scale,
		height: 22 * scale,
		position: 'absolute',
		left: 40 * scale,
		top: 8 * scale,
		zIndex: 9,
		opacity: 0.8,
	},
	listItem: {
		backgroundColor: '#fff',
		borderRadius: 4 * scale,
		marginBottom: 22 * scale
	},
	clickButtom: {
		backgroundColor: '#fff',
		borderRadius: 5 * scale,
		paddingHorizontal: 10 * scale,
		paddingVertical: 12 * scale,
		flexDirection: 'row',
		alignItems: 'center',
		justifyContent: 'space-between',
		marginTop: 15 * scale
	},
})